package com.ffcs.cn.action.base.treeTable;

import com.ffcs.cn.DevelopProperties;
import com.supermap.desktop.core.utilties.StringUtilities;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
  * 树节点类，继承自DefaultMutableTreeTableNode，职责如下：
 * 1、getChildCount、getChildAt、refreshNodeState、getChildCountNew、isNodeVisible 都是为了实现搜索时，不匹配的树节点过滤掉不显示，
 *    所以需要重写getChildCount getChildAt方法，比如：当前节点下有5个节点，其中三个节点跟搜索文本匹配，另外两个节点就得过滤掉，如果不重写getChildCount 始终返回5个就没办法过滤了，
 *    getChildCount 返回了3个，getChildAt 也得重写返回匹配的三个，refreshNodeState 是根据搜索内容刷新节点的方法；getChildCountNew 根据搜索内容获取当前节点下子节点的个数，
 *    isNodeVisible 当前节点是否可显示
 * 2. getColumnCount 返回列个数，注意得跟DialogJTreeTable.DefaultTreeTableModelDemo.getColumnCount匹配
 * 3. isEditable 返回可编辑的列，目前是第四列可编辑
 * 4. getValueAt 返回值，直接返回树节点本身，比较好操作，注意：跟渲染器、DialogJTreeTable.DefaultTreeTableModelDemo.getColumnClass要注意匹配
 * 5. getRenderComponent 根据列返回对应的渲染器需要的渲染控件，主要提供给渲染器和DialogJTreeTable里的鼠表格控件的鼠标点击事件用
 */
public class TreeTableNode extends DefaultMutableTreeTableNode {
	private boolean tempVisible = true;// 搜索中是否可见
	private static String searchText;

	private TreeTableData treeTableData;
	private PanelCheck panelCheck = null;
	private JButton buttonDelete = null;
	private JButton buttonAdd = null;
	private JComboBox jComboBox = null;


	public TreeTableNode(TreeTableData treeTableData) {
		this.treeTableData = treeTableData;
	}

	public static String getSearchText() {
		return searchText;
	}

	public static void search(String text) {
		searchText = text;
	}

	@Override
	public int getColumnCount() {
		return 4;
	}

	@Override
	public boolean isEditable(int column) {
		return column == DialogJTreeTable.COLUMN_INDEX_TIME;
	}

	@Override
	public TreeTableNode getValueAt(int column) {
		return this;
	}

	public Component getRenderComponent(int index) {
		if (index == DialogJTreeTable.COLUMN_INDEX_NAME) {
			this.panelCheck = new PanelCheck(this.treeTableData);
			return this.panelCheck;
		} else if (index == DialogJTreeTable.COLUMN_INDEX_DELETE) {
			if (this.buttonDelete == null) {
				this.buttonDelete = new JButton(DevelopProperties.getString("String_Delete"));
			}
			return this.buttonDelete;
		} else if (index == DialogJTreeTable.COLUMN_INDEX_ADD) {
			if (this.buttonAdd == null) {
				this.buttonAdd = new JButton(DevelopProperties.getString("String_Add"));
			}
			return this.buttonAdd;
		} else if (index == DialogJTreeTable.COLUMN_INDEX_TIME) {
			if (this.jComboBox == null) {
				this.jComboBox = new JComboBox();
				this.jComboBox.addItem("2020");
				this.jComboBox.addItem("2021");
				this.jComboBox.addItem("2022");
				this.jComboBox.addItem("2023");
				this.jComboBox.addItem("2024");
				this.jComboBox.addItemListener(new ItemListener() {
					@Override
					public void itemStateChanged(ItemEvent e) {
						if (e.getStateChange() == ItemEvent.SELECTED) {
							treeTableData.setTime(jComboBox.getSelectedItem().toString());
						}
					}
				});
			}
			return this.jComboBox;
		}
		return null;
	}

	public TreeTableData getTreeTableData() {
		return this.treeTableData;
	}

	@Override
	public int getChildCount() {
		int count = 0;
		if (children != null) {
			for (Object child : children) {
				TreeTableNode processTreeNode = (TreeTableNode) child;
				if (processTreeNode.isNodeVisible()) {
					count++;
				}
			}
		}
		return count;
	}

	@Override
	public TreeTableNode getChildAt(int index) {
		int tempIndex = -1;
		for (Object child : children) {
			TreeTableNode processTreeNode = (TreeTableNode) child;
			if (processTreeNode.isNodeVisible()) {
				tempIndex++;
			}
			if (tempIndex == index) {
				return processTreeNode;
			}
		}
		return null;
	}

	public boolean refreshNodeState() {
		this.tempVisible = StringUtilities.isNullOrEmpty(searchText) || StringUtilities.isContainWithPinYin(this.treeTableData.getValue(), searchText);
		if (getChildCountNew() > 0) {
			this.tempVisible = true;
		}
		return this.tempVisible;
	}

	private int getChildCountNew() {
		int count = 0;
		if (children != null) {
			for (Object child : children) {
				TreeTableNode processTreeNode = (TreeTableNode) child;
				if (processTreeNode.refreshNodeState()) {
					count++;
				}
			}
		}
		return count;
	}

	private boolean isNodeVisible() {
		return StringUtilities.isNullOrEmpty(searchText) || this.tempVisible;
	}

}
